/*
 * @Author: GKN
 * @Date: 2023-07-27 11:08:51
 * @LastEditTime: 2024-01-15 16:12:53
 * @LastEditors: GKN
 * @Description: 
 * @FilePath: \mod\src\api\http.js
 * 可以输入预定的版权声明、个性签名、空行等
 */
import axios from 'axios'
import { ElNotification } from 'element-plus'
import CryptoJS from 'crypto-js';
import router from '@/router'

const baseURL = 'http://api.xdqycx.com/api/v1/'
const httpApi = {}
const sha256 = (str, nowTm) => {
  return CryptoJS.SHA256(str + "tm:" + nowTm).toString()
}
const axiosInit = axios.create({
  // 设置超时时间
  time:2000,
  headers: {
        'Content-Type': 'application/json',
    },//请求头
  })
  
  // 2.请求拦截器
  axiosInit.interceptors.request.use((config) => {
    //在发送之前做点什么
    return config
  }, (error) => {
    //对请求错误做点什么
    return error
  })
  
  // 3.相应拦截器
  axiosInit.interceptors.response.use(response => {
      const data = response;
      if(data.status == 401){
        
      }else{
        return response;
      }
      
  }, error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          return error.response.data;
      }
    }
    return Promise.reject(error.response.data)
  })
httpApi.request = (apiUrl, type,data,isMsg=false, succCallback, errCallback) => {
  var param = "{}"
  if(type == 'get'){
    var ul=apiUrl.split("?")
    if (ul.length>1){
      param=ul[1]
    }
  }else if(type == 'post'||type == 'put'||type == 'delete'){
    if (data != {}) {
      param =JSON.stringify(data)
    }
    
  }else{}
  var nowTm=parseInt(new Date().valueOf() / 1000)
  axiosInit.defaults.headers.common['x-tm'] = nowTm
  axiosInit.defaults.headers.common['x-sign'] = sha256(param,nowTm)
  const loginData = JSON.parse(localStorage.getItem( 'loginData' ))
  
  if(loginData){
    axiosInit.defaults.headers.common['authorization'] = 'Bearer ' +loginData.authorization
    axiosInit.defaults.headers.common['x-userid'] = loginData.xUserid
    axiosInit.defaults.headers.common['x-sessionid'] = loginData.xSessionid
  }
  let httpUrl = baseURL + apiUrl
  if(type == 'get'){
    axiosInit.get(httpUrl).then(
      (res) => {
        if (succCallback) {
          if(res.msg == '登录已过期'){
            localStorage.removeItem( 'loginData' );
            localStorage.removeItem( 'user' );
          }
          succCallback(res)
        } else {
         
        }
      },
      (err) => {
        if (errCallback) {
          errCallback(err)
        }
      }
    ).catch(error =>{
      
      
    })
  }else if(type == 'post'){
    axiosInit.post(httpUrl,data).then(
      (res) => {
        if (succCallback) {
          if(isMsg&&res!=undefined){
            if(res.data.code!= 0){
              ElNotification({
                message: res.data.msg,
                type: 'error',
              })
              errCallback(res)
            }else{
              succCallback(res)
            }
          }else{
            succCallback(res)
          }
          
        } else {
        }
      },
      (err) => {
        if (errCallback) {
          errCallback(err)
        }
      }
    )
  }else if(type == 'put'){
    axiosInit.put(httpUrl,data).then(
      (res) => {
        if (succCallback) {
          if(isMsg&&res!=undefined){
            if(res.data.code!= 0){
              ElMessage.error( res.data.msg)
            }else{
              succCallback(res)
            }
          }else{
            succCallback(res)
          }
          
        } else {
        }
      },
      (err) => {
        if (errCallback) {
          errCallback(err)
        }
      }
    )
  }else{}
  
}
export default httpApi

